﻿using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace buildingLib
{
    [Serializable()]
    public class Member:BusinessBase<Member>
    {
        #region Business Methods
        int m_id = 0;
        string m_name = string.Empty;
        bool m_gender;
        string m_relation = string.Empty;
        string m_IDCard = string.Empty;
        string m_company = string.Empty;
        string m_tel = string.Empty;
        string m_memo = string.Empty;
        bool m_contactor = false;
        Guid m_ownerID;


        public int ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_name != value)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }

        public bool Gender
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_gender;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);                
                if (m_gender != value)
                {
                    m_gender = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Relation
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_relation;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_relation != value)
                {
                    m_relation = value;
                    PropertyHasChanged();
                }
            }
        }

        public string IDCard
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_IDCard;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_IDCard != value)
                {
                    m_IDCard = value;
                    PropertyHasChanged();
                }
            }
        }

        public string Company
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_company;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_company != value)
                {
                    m_company = value;
                    PropertyHasChanged();
                }
            }
        }

        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }

        public string Tel
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_tel;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_tel != value)
                {
                    m_tel = value;
                    PropertyHasChanged();
                }
            }
        }

        /// <summary>
        /// 是否为紧急联系人
        /// </summary>
        public bool IsContactor
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_contactor;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);                
                if (m_contactor != value)
                {
                    m_contactor = value;
                    PropertyHasChanged();
                }
            }
        }

        public Guid OwnerID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_ownerID;
            }
        }


        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation
        protected override void AddBusinessRules()
        {
            ValidationRules.AddRule(
              Csla.Validation.CommonRules.StringRequired, "Name");
            ValidationRules.AddRule(
              Csla.Validation.CommonRules.StringMaxLength,
              new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
        }
        #endregion

        #region Factory Methods
        public static Member NewMember(Guid ownerid)
        {
            return new Member(ownerid);
        }

        public static Member GetMember(SafeDataReader dr)
        {
            return new Member(dr);
        }
        
        private Member(Guid ownerid)
        {
            MarkAsChild();
            m_ownerID = ownerid;
        }
        private Member(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }
        #endregion

        #region DataAccess
        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetInt32("id");
            m_name = dr.GetString("name");
            m_gender = dr.GetBoolean("gender");
            m_relation = dr.GetString("relation");
            m_IDCard = dr.GetString("idcard");
            m_company = dr.GetString("company");
            m_tel = dr.GetString("tel");
            m_ownerID = dr.GetGuid("ownerid");
            m_contactor = dr.GetBoolean("contactor");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GInsertMember");
            db.AddOutParameter(dbcom, "id", DbType.Int32, 4);
            DoInsertUpdate(dbcom);

            m_id = (int)db.GetParameterValue(dbcom, "id");

            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GUpdateMember");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);
            
            DoInsertUpdate(dbcom);

            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "name", DbType.String, m_name);
            db.AddInParameter(dbcom, "gender", DbType.Boolean, m_gender);
            db.AddInParameter(dbcom, "relation", DbType.String, m_relation);
            db.AddInParameter(dbcom, "tel", DbType.String, m_tel);
            db.AddInParameter(dbcom, "idcard", DbType.String, m_IDCard);
            db.AddInParameter(dbcom, "company", DbType.String, m_company);
            db.AddInParameter(dbcom, "ownerID", DbType.Guid, m_ownerID);
            db.AddInParameter(dbcom, "contactor", DbType.Boolean, m_contactor);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GDeleteMember");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }
        #endregion
    }
}
